<?php

use App\Modules\Good\Models\GoodSpec;
use App\Modules\Order\Models\Order;
use App\Modules\Rebate\Jobs\RebateJob;
use App\Modules\System\RuleIntroduce;
use App\Modules\User\Manager;
use Carbon\Carbon;
use Illuminate\Foundation\Inspiring;
use Silber\Bouncer\BouncerFacade;

/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
Artisan::command('test', function () {

    Order::query()
//        ->where('created_at', '>', now()->subDays(15)->toDateTimeString())
        ->whereNotNull('referrer_id')
        ->where('referrer_level', '>', 0)
        ->where('status', Order::STATUS_PAID)
        ->selectRaw('sum(original_price) as money, count(1) as count, date_format(created_at, "%Y-%m-%d") as date,referrer_level, referrer_id, shop_id')
        ->groupBy(['referrer_level', 'referrer_id', 'shop_id', 'date'])
        ->orderBy('referrer_id')
        ->each(function ($item) {
            var_export($item->date);
            \App\Modules\System\Models\Rank::query()
                ->updateOrCreate([
                    'user_id' => $item->referrer_id,
                    'shop_id' => $item->shop_id,
                    'level' => $item->referrer_level,
                    'date' => $item->date,
                ], [
                    'money' => $item->money,
                    'number' => $item->count,
                ]);
        });
});

Artisan::command('update:displayPrice', function () {
    GoodSpec::query()
        ->orderBy('id')
        ->each(function ($item) {
            $item->display_price = $item->price;
            $item->save();
        });
});

Artisan::command('update:shopId', function () {
    \App\Modules\User\Models\User::query()
        ->whereNotNull('shop_id')
        ->get()
        ->each(function ($item) {
//            if (is_array($item->shop_id)) {
//                $item->shop_id = null;
//                $item->save();
//            }
            $item->shop_id = [$item->shop_id];
            $item->save();
        });
});
